<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css"/>
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="layui-form-item">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-block">
                    <input type="text" name="pname" lay-verify="pname" autocomplete="off" placeholder="请输入姓名"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">地域</label>
                <div class="layui-input-block">
                    <input type="text" name="pos" lay-verify="pos" autocomplete="off"
                           placeholder="请输入地域" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">擅长领域</label>
                <div class="layui-input-block">
                    <input type="text" name="domain" lay-verify="domain" autocomplete="off"
                           placeholder="请输入擅长领域" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-card-body">
                    <textarea name="edit" id="file" cols="30" rows="10"></textarea>
                    <div class="layui-btn-group" style="margin-top: 10px">
                        <button class="layui-btn" lay-event="getContent">介绍</button>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">启用状态</label>
                <div class="layui-input-block">
                    <input type="radio" name="isvalid" value="true" title="启用" checked>
                    <input type="radio" name="isvalid" value="false" title="禁用">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">专家类型</label>
                <div class="layui-input-block">
                    <select name="ptype" lay-verify="ptype">
                        <option value="">请选择</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">专家图片</label>
                <div class="layui-input-block">
                    <input type="text" name="coverimg" lay-verify="coverimg" autocomplete="off" placeholder=""
                           id="coverimg" class="layui-input">
                    <input class="btn-upload file-up" style="display: inline-block;" type="file"
                           accept="image/*" name="edit" id="img" value="上传">
                </div>
                <div class="layui-input-block">
                    <img id="preview" height="200px" width="300px" src="#" alt="没有选择" />
                    <span id="demoText"></span>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script src="../../../component/layui/layui.js"></script>
<script src="../../../component/pear/pear.js"></script>
<script>
    layui.use(['form', 'jquery', 'laydate', 'tinymce','laytpl','upload'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        var tinymce = layui.tinymce;
        var laytpl = layui.laytpl;
        var upload = layui.upload;
        var laydate = layui.laydate;
        let MODULE_PATH = "operate/";

        $.ajaxSetup({
            headers: {
                'Authorization': window.sessionStorage.getItem("token")
            }
        });

        var file = tinymce.render({
            elem: "#file",
            height: 400,
            automatic_uploads: true,
            images_upload_url: window.BASEPATH + "upload.do",
            images_upload_base_path: window.BASEPATH
        });

        var uploadInst = upload.render({
            elem: '#img',
            url: window.BASEPATH + 'upload2'
            ,
            auto: true //选择文件后不自动上传
            ,
            choose: function(obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result) {
                    $('#preview').attr('src', result); //图片链接（base64）
                });
            },
            before: function(obj) {
                layer.msg('上传中', {
                    icon: 16,
                    time: 0
                });
            },
            done: function(res) {
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
                $("#coverimg").val(res.data);
                //上传成功的一些操作
                layer.msg("信息上传成功！", {
                    time: 3000
                });
            },
            error: function() {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html(
                    '<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>'
                );
                demoText.find('.demo-reload').on('click', function() {
                    uploadInst.upload();
                });
            }
        });

        $.ajax({
            url: window.BASEPATH + 'team/findAll',
            type: 'GET',
            dataType: 'json',
            success: function (res) {
                $('select[name="ptype"]').empty();
                if (res.code == 0) {
                    var data = res.data;
                    console.log(data);
                    for (var i = 0; i < data.length; i++) {
                        var option = '<option value="' + data[i].id + '">' + data[i].tname + '</option>';
                        console.log(option);
                        $('select[name="ptype"]').append(option);
                    }
                    form.render('select');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            },
            error: function (xhr, textStatus, errorThrown) {
                layer.msg('网络错误', {icon: 2});
            }
        });

        form.on('submit(user-save)', function (data) {
            data.field.intro = file.getContent();
            $.ajax({
                url: window.BASEPATH + 'professor/add',
                data: data.field,
                type: 'post',
                success: function (result) {
                    if (result.code==0) {
                        layer.msg(result.msg, {
                            icon: 1,
                            time: 1000
                        }, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window
                                .name)); //关闭当前页
                            parent.layui.table.reload("user-table");
                        });
                    } else {
                        layer.msg(result.msg, {
                            icon: 2,
                            time: 3000
                        });
                    }
                }
            })
            return false;
        });


    })
</script>
<script>
</script>
</body>
</html>